# -*- coding: utf-8 -*-
import scrapy
from baidutop.items import BaidutopItem
from scrapy.contrib.loader import ItemLoader
from scrapy.selector import Selector



class BaidutopSpider(scrapy.Spider):
    name = "BaiduTop"
    #allowed_domains = ["baidu.com"]
    start_urls = ['http://top.baidu.com/buzz?b=1&fr=topindex']

    '''
    def start_requests(self):
        return [scrapy.FormRequest("http://www.example.com/login",
                                   formdata={'user': 'john', 'pass': 'secret'},
                                   callback=self.logged_in)]
    '''
    def parse(self, response):
        newsSelector = Selector(response=response).xpath('//table[@class="list-table"]/tr')
        for baiduitem in newsSelector:
            baiduItem = ItemLoader(item = BaidutopItem(), selector=baiduitem)
            if(len(baiduitem.xpath('td[@class="first"]')) > 0):
                baiduItem.add_xpath('title', 'td[@class="keyword"]/a[@class="list-title"]/text()')
                baiduItem.add_xpath('hot', 'td[@class="last"]/span/text()')
                baiduItem.add_xpath('link', 'td[@class="keyword"]/a[@class="list-title"]/@href')
                yield self.addNews(baiduItem)
        

    def addNews(self, item):
        return item.load_item()